package cn.yunhe.util;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;


public class BaseActionUtil extends ActionSupport {

    /**
     * 解耦的方式访问 Request 和 Session
     *
     * @return
     */
    public Map<String,Object> getRequestMap(){
        return (Map<String, Object>) ActionContext.getContext().get("request");
    }

    public Map<String,Object> getSessionMap(){
        return ActionContext.getContext().getSession();
    }



    /**
     * 耦合的方式 访问response,request,Session
     *
     * 注意：
     * 在获取response的同时必须给其设置 编码格式
     *
     * @return
     */
    public HttpServletResponse getResponse(){
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/html;charset=UTF-8");
        return response;
    }

    public HttpServletRequest getRequest(){
        return ServletActionContext.getRequest();
    }

    public HttpSession getSession(){
        return ServletActionContext.getRequest().getSession();
    }
}
